package controllers;

import java.lang.String;import java.util.List;

import beans.PageBean;
import models.Article;
import models.Photo;
import models.Recommend;
import play.mvc.Controller;
import play.mvc.Scope;
import play.mvc.With;
import utils.MessageUtil;
import utils.StringUtils;
@With(value=CheckLogin.class)
public class Recommends extends Controller{
		/**
		 * 添加推荐的页面
		 */
		public static void add() {
			render();
		}
		/**
		 * 添加推荐
		 * @param keyId
		 * @param photoId
		 * @param name
		 * @param weight
		 */
		public static void addRecommend(String keyId,String photoId,String name,int weight){
			if(!StringUtils.isEmpty(photoId)&&!StringUtils.isEmpty(keyId)&&!StringUtils.isEmpty(name)){

                Recommend productrecommended = new Recommend();
				productrecommended.keyId = keyId;
				productrecommended.name = name;
				productrecommended.photo_id = photoId;
				productrecommended.weight = weight;
				productrecommended.save();
				MessageUtil.generateInfoMsg("发布成功");
				ShowRecommedList(0);
			}
			else{
				MessageUtil.generateErrorMsg("发布失败");
				ShowRecommedList(0);
			}
		}

		/**
		 * 推荐列表
		 * @param page
		 */
		public static void ShowRecommedList(int page){
			long size = Recommend.count("type = ?",0);
			PageBean pagebean = PageBean.getInstance(page, size);
			List<Recommend> productrecommendedList = Recommend.find("type = ?",0).fetch(pagebean.getCurPage(),pagebean.getPerPage());
            render(productrecommendedList,pagebean);
            MessageUtil.cleanMsg(Scope.Flash.current());
		}
		/**
		 * 删除推荐
		 * @param id
		 * @param type
		 */
		public static void delete(String id,int type){
            Recommend productrecommended = Recommend.findById(id);
			if(productrecommended!=null){
				productrecommended.delete();
				MessageUtil.generateInfoMsg("删除成功");
				ShowRecommedList(0);
			}
			else{
				MessageUtil.generateErrorMsg("删除失败");
				ShowRecommedList(0);
			}
			
		}
		/**
		 * 修改推荐的页面
		 * @param id
		 */
		public static void modify(String id){
            Recommend productrecommended = Recommend.findById(id);
			if (productrecommended!=null) {
				render(productrecommended);
			} else {
				MessageUtil.generateErrorMsg("不能修改");
				ShowRecommedList(0);
			}
			render();
		}
		/**
		 * 修改推荐的方法
		 * @param keyId
		 * @param photoId
		 * @param type
		 * @param name
		 * @param id
		 * @param weight
		 */
		public static void modifyRecommend(String keyId,String photoId,int type,String name,String id,
				int weight){
			if(!StringUtils.isEmpty(photoId)&&!StringUtils.isEmpty(keyId)&&
					(type==0||type==1)&&!StringUtils.isEmpty(name)&&!StringUtils.isEmpty(id)){
                Recommend productrecommended = Recommend.findById(id);
				if(productrecommended!=null){
					productrecommended.keyId = keyId;
					productrecommended.name = name;
					productrecommended.photo_id = photoId;
					productrecommended.weight = weight;
					productrecommended.save();
					MessageUtil.generateInfoMsg("修改成功");
					ShowRecommedList(0);
				}
				else{
					MessageUtil.generateErrorMsg("修改失败");
					ShowRecommedList(0);
				}
			}
			else{
				MessageUtil.generateErrorMsg("字段不能为空");
				ShowRecommedList(0);
			}
		}
		/**
		 * 显示推荐信息
		 * @param id
		 * @param type
		 */
		public static void show(String id,int type) {
            Recommend productrecommended = Recommend.findById(id);
			if (productrecommended!=null) {
                Article article = Article.findById(productrecommended.keyId);
                Photo photo = Photo.findById(productrecommended.photo_id);
				render(article,photo,productrecommended);
			} else {
				MessageUtil.generateErrorMsg("信息不存在");
				ShowRecommedList(0);
			}
			render();
            MessageUtil.cleanMsg(Scope.Flash.current());
        }

}
